<?php
namespace app\sysAdmin\controller;
use app\common\Base;
use app\common\Compent;
use app\common\Emodel;
use app\common\TeacherModel;
use app\common\HttpClient;
use think\Db;

class TeacherOrganizeManage extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$where = [];
		$grade = input("grade");
		$subject = input("subjectId");
		$name = input("teacherName");
		$page = input("page");
		$rows = input("rows");
		if($grade) {
			$array = explode("_", $grade);
			$where["年级号"] = array("eq", $array[0]);
			$where["学籍类型"] = array("eq", $array[1]);
		}
		if($subject) $where["任教学科ID"] = array("eq", $subject);
		if($name) $where["姓名"] = array("like", $name."%");
		$fields = [];
		$Model = new TeacherModel();
		return $Model->getTeacherList($page, $rows, $fields, $where);
	}

	public function addUpdate() {
		$all = request()->param();
		try {
			if(array_key_exists("年级号", $all)) {
				$array = explode("_", $all["年级号"]);
				$all["年级号"] = $array[0];
				$all["学籍类型"] = $array[1];
			}
			if(array_key_exists("职位", $all)) {
				$data = Db::table("t_organize_depart")->where("PARENT_ID > 0")->select();
				$array = explode(",", $all["职位"]);
			    $result = [];
			    foreach ($array as $value) {
			        foreach ($data as $obj) {
			        	if($obj["ID"] == $value && !in_array($obj["PARENT_ID"], $result)) {
							array_push($result, $obj["PARENT_ID"]);
							break;
			        	}
			        }
			    }
			    if(count($result) > 0) {
			    	$all["部门"] = implode(",", $result);
			    }
			}
			if(array_key_exists("ID", $all)) {
				update('教师信息', $all);
			} else {
				insert('教师信息', $all);
			}
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function wechatUnbind() {
		try {
			$id = input("id");
			Db::table('教师信息')
			->where('ID',$id)
			->update(['微信ID2' => null,'头像' => null]);
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function createBindEwm() {
		try {
			$id = input("id");
			$data["teacherId"] = $id;
			$data["appid"] = "wx6cba9644554aff95";
			$url = "http://yxt.smsyzx.org/wechat/getTeacherBindEwm";
			$http = new HttpClient();
			$httpstr = $http->http($url, $data, 'GET');
			return $httpstr;
		} catch (Exception $e) {
			$error = $e->getMessage();
			$data["code"] = "999";
			$data["msg"] = $error;
			return $data;
		}
	}

}
